package com.example.administrator.baikangxing.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.example.administrator.baikangxing.MyApplication;
import com.example.administrator.baikangxing.config.Constants;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Sushil_Shen on 2017/4/19/0019.
 * Contact: Sushil_shen@163.com
 */

public class Utils {

    /*获取末尾字符*/
    public static String getLastwords(String srcText, String p) {
        try {
            String[] array = TextUtils.split(srcText, p);
            int index = (array.length - 1 < 0) ? 0 : array.length - 1;
            return array[index];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 获取设备的IMEI */
    public static String getIMEI() {
        Context context = MyApplication.context;
        if (null == context) {
            return null;
        }
        String imei = null;
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            imei = tm.getDeviceId();
        } catch (Exception e) {
            LogUtil.e(String.valueOf(e));
        }
        return imei;
    }

    private String getVersion() {
        PackageManager packageManager = MyApplication.context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(Constants.APP_PACKAGE_NAME, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String versionName = packageInfo.versionName;
        return versionName;
    }

    /*拼接token*/
    public static void getToken(){

    }
    //获取里程和消耗
    //0代表路程，1代表消耗
    public static String calc(int type,int i){//type 0 return mails,1 return carle
        float mails = i * 0.65f / 1000;
        DecimalFormat df = new DecimalFormat("0.0");
        String format = df.format(mails);
        if (type == 0){
            return format;
        }else if (type == 1){
            int carle = (int)(50 * mails * 0.82f);//体重（kg）* 距离（km）* 运动系数（k）
            return carle+"";
        }
        return "";
    }
    //获取绝对值
    public static int getAbs(String num){
        int abs = Math.abs(Integer.valueOf(num));
        return abs;
    }

    //先转换float，再转int
    public static int stringToInt(String num){
        int i = Float.valueOf(num).intValue();
        return i;
    }

}
